﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using HangerSupport.Properties;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace HangerSupport
{
	public partial class DlgAddSeismicComponent : System.Windows.Forms.Form
	{
		public SeismicComponentModel Scm
		{
			get
			{
				this.BingSeismicComponentInfomation();
				return this.scm;
			}
			set
			{
				this.scm = value;
			}
		}

		public bool CmdEsc
		{
			get
			{
				return this.cmdEsc;
			}
			set
			{
				this.cmdEsc = value;
			}
		}

		public HSType HsType
		{
			get
			{
				this.BingHangerType();
				return this.hsType;
			}
			set
			{
				this.hsType = value;
			}
		}

		public DlgAddSeismicComponent()
		{
			this.InitializeComponent();
			YJKProcessTools hyprocessTools = new YJKProcessTools();
			hyprocessTools.ShowInTaskbarChange(this);
			base.Icon = YArchitech.LIB.Resources.YArchitech;
			this.scm = new SeismicComponentModel();
			this.userSetting = new UserSetting();
			this.beLoad = true;
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void DlgAddSeismicComponent_Load(object sender, EventArgs e)
		{
			this.LoadPreviwImage();
			base.KeyPreview = true;
			this.Initializate();
		}

		private void tabHangerType_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool flag = this.tabHangerType.SelectedIndex == 2;
			if (flag)
			{
				this.chkLocationE.Enabled = false;
				this.chkLocationF.Enabled = false;
			}
			else
			{
				this.chkLocationE.Enabled = true;
				this.chkLocationF.Enabled = true;
			}
			bool flag2 = !this.beLoad;
			if (flag2)
			{
				this.cmdEsc = false;
				this.MimicEsc();
			}
			this.beLoad = false;
		}

		private void chkLocationA_CheckedChanged(object sender, EventArgs e)
		{
			CheckBox checkBox = sender as CheckBox;
			bool flag = checkBox != null;
			if (flag)
			{
				bool @checked = checkBox.Checked;
				if (@checked)
				{
					string name = checkBox.Name;
					if (!(name == "chkLocationA"))
					{
						if (!(name == "chkLocationB"))
						{
							if (!(name == "chkLocationC"))
							{
								if (!(name == "chkLocationD"))
								{
									if (!(name == "chkLocationE"))
									{
										if (name == "chkLocationF")
										{
											this.hyTxtAngleF.Enabled = true;
										}
									}
									else
									{
										this.hyTxtAngleE.Enabled = true;
									}
								}
								else
								{
									this.hyTxtAngleD.Enabled = true;
								}
							}
							else
							{
								this.hyTxtAngleC.Enabled = true;
							}
						}
						else
						{
							this.hyTxtAngleB.Enabled = true;
						}
					}
					else
					{
						this.hyTxtAngleA.Enabled = true;
					}
				}
				else
				{
					string name2 = checkBox.Name;
					if (!(name2 == "chkLocationA"))
					{
						if (!(name2 == "chkLocationB"))
						{
							if (!(name2 == "chkLocationC"))
							{
								if (!(name2 == "chkLocationD"))
								{
									if (!(name2 == "chkLocationE"))
									{
										if (name2 == "chkLocationF")
										{
											this.hyTxtAngleF.Enabled = false;
										}
									}
									else
									{
										this.hyTxtAngleE.Enabled = false;
									}
								}
								else
								{
									this.hyTxtAngleD.Enabled = false;
								}
							}
							else
							{
								this.hyTxtAngleC.Enabled = false;
							}
						}
						else
						{
							this.hyTxtAngleB.Enabled = false;
						}
					}
					else
					{
						this.hyTxtAngleA.Enabled = false;
					}
				}
			}
			bool flag2 = this.cmdEsc;
			if (flag2)
			{
			}
		}

		private void cmbComponent_SelectedIndexChanged(object sender, EventArgs e)
		{
			string text = (sender as System.Windows.Forms.ComboBox).Text;
			int num = text.IndexOf('(');
			int num2 = text.IndexOf(')');
			string text2 = text.Substring(num + 1, text.Length - num - 2);
			string[] array = text2.Split(new char[]
			{
				'×'
			}, StringSplitOptions.RemoveEmptyEntries);
			bool flag = array.Length == 3;
			if (flag)
			{
				this.hyTxtWidthB.Text = array.ElementAt(0);
				this.hyTxtHightH.Text = array.ElementAt(1);
				this.hyTxtThickT.Text = array.ElementAt(2);
				this.hyTxtWidthB.Enabled = false;
				this.hyTxtHightH.Enabled = false;
				this.hyTxtThickT.Enabled = false;
			}
			else
			{
				this.hyTxtWidthB.Enabled = true;
				this.hyTxtHightH.Enabled = true;
				this.hyTxtThickT.Enabled = true;
				this.hyTxtWidthB.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "WidthB")) ? "41" : this.userSetting.GetValue("AddSeismicComponent", "WidthB"));
				this.hyTxtHightH.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "HightH")) ? "41" : this.userSetting.GetValue("AddSeismicComponent", "HightH"));
				this.hyTxtThickT.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "ThickT")) ? "2" : this.userSetting.GetValue("AddSeismicComponent", "ThickT"));
			}
			bool flag2 = this.cmdEsc;
			if (flag2)
			{
			}
		}

		private void DlgAddSeismicComponent_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.WriteData();
			this.MimicEsc();
			this.cmdEsc = true;
		}

		private void DlgAddSeismicComponent_KeyPress(object sender, KeyPressEventArgs e)
		{
			bool flag = e.KeyChar == '\u001b';
			if (flag)
			{
				base.Close();
			}
		}

		private void hyTxtWidthB_TextChanged(object sender, EventArgs e)
		{
		}

		private void hyTxtAngleD_TextChanged(object sender, EventArgs e)
		{
		}

		private void hyTxtAngleF_Validating(object sender, CancelEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			try
			{
				bool flag = textBox != null;
				if (flag)
				{
					double num;
					bool flag2 = double.TryParse(textBox.Text, out num) && num >= 22.5 && num <= 67.5;
					if (!flag2)
					{
						YJKMessageBox.Information("“抗震组件角度”需是在22.5°～67.5°范围内的有效数字!");
						textBox.Focus();
						e.Cancel = true;
					}
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Information("“抗震组件角度”需是在22.5°～67.5°范围内的有效数字!");
				textBox.Focus();
				e.Cancel = true;
			}
		}

		private void hyTxtThickT_Validating(object sender, CancelEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			try
			{
				bool flag = textBox != null && textBox.Enabled;
				if (flag)
				{
					double num;
					bool flag2 = !double.TryParse(textBox.Text, out num) || num == 0.0;
					if (flag2)
					{
						throw new Exception();
					}
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Information("您输入的“抗震组件尺寸”不满足创建要求，无法生成抗震组件!");
				textBox.Focus();
				e.Cancel = true;
			}
		}

		private void MimicEsc()
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			DlgAddSeismicComponent.SendMessage(cmdAddSeismicComponent.wndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			DlgAddSeismicComponent.keybd_event(bVk, 0, dwFlags, 0);
			DlgAddSeismicComponent.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void BingSeismicComponentInfomation()
		{
			this.BingLayLocation();
			this.BingSeismicComponentInformation();
		}

		private void BingHangerType()
		{
			switch (this.tabHangerType.SelectedIndex)
			{
			case 0:
				this.scm.hsType = (this.hsType = HSType.kDJ_DC);
				break;
			case 1:
				this.scm.hsType = (this.hsType = HSType.kDJ_GJ);
				break;
			case 2:
				this.scm.hsType = (this.hsType = HSType.kDJ_DG);
				break;
			}
		}

		private void BingLayLocation()
		{
			this.scm.location_A_Angle = ((this.hyTxtAngleA.Enabled && !string.IsNullOrEmpty(this.hyTxtAngleA.Text.Trim())) ? new KeyValuePair<string, double>("Location_A", Convert.ToDouble(this.hyTxtAngleA.Text.Trim())) : new KeyValuePair<string, double>("Location_A", 0.0));
			this.scm.location_B_Angle = ((this.hyTxtAngleB.Enabled && !string.IsNullOrEmpty(this.hyTxtAngleB.Text.Trim())) ? new KeyValuePair<string, double>("Location_B", Convert.ToDouble(this.hyTxtAngleB.Text.Trim())) : new KeyValuePair<string, double>("Location_B", 0.0));
			this.scm.location_C_Angle = ((this.hyTxtAngleC.Enabled && !string.IsNullOrEmpty(this.hyTxtAngleC.Text.Trim())) ? new KeyValuePair<string, double>("Location_C", Convert.ToDouble(this.hyTxtAngleC.Text.Trim())) : new KeyValuePair<string, double>("Location_C", 0.0));
			this.scm.location_D_Angle = ((this.hyTxtAngleD.Enabled && !string.IsNullOrEmpty(this.hyTxtAngleD.Text.Trim())) ? new KeyValuePair<string, double>("Location_D", Convert.ToDouble(this.hyTxtAngleD.Text.Trim())) : new KeyValuePair<string, double>("Location_D", 0.0));
			this.scm.location_E_Angle = ((this.hyTxtAngleE.Enabled && !string.IsNullOrEmpty(this.hyTxtAngleE.Text.Trim())) ? new KeyValuePair<string, double>("Location_E", Convert.ToDouble(this.hyTxtAngleE.Text.Trim())) : new KeyValuePair<string, double>("Location_E", 0.0));
			this.scm.location_F_Angle = ((this.hyTxtAngleF.Enabled && !string.IsNullOrEmpty(this.hyTxtAngleF.Text.Trim())) ? new KeyValuePair<string, double>("Location_F", Convert.ToDouble(this.hyTxtAngleF.Text.Trim())) : new KeyValuePair<string, double>("Location_F", 0.0));
		}

		private void BingSeismicComponentInformation()
		{
			this.scm.componentSpecification = this.cmbComponent.SelectedText;
			this.scm.SectionType = this.hyTxtType.Text;
			this.scm.widthB = (string.IsNullOrEmpty(this.hyTxtWidthB.Text.Trim()) ? 0.0 : Convert.ToDouble(this.hyTxtWidthB.Text.Trim()));
			this.scm.hightH = (string.IsNullOrEmpty(this.hyTxtHightH.Text.Trim()) ? 0.0 : Convert.ToDouble(this.hyTxtHightH.Text.Trim()));
			this.scm.thickT = (string.IsNullOrEmpty(this.hyTxtThickT.Text.Trim()) ? 0.0 : Convert.ToDouble(this.hyTxtThickT.Text.Trim()));
		}

		private void InitializateCmbComponent()
		{
			List<string> list = new List<string>();
			list.Add("槽钢(21×21×2)");
			list.Add("槽钢(31×31×2)");
			list.Add("槽钢(41×41×2)");
			list.Add("槽钢(41×41×3)");
			list.Add("槽钢(52×52×2.5)");
			list.Add("槽钢(72×72×2.75)");
			list.Add("自定义");
			this.cmbComponent.DataSource = list;
			this.cmbComponent.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "cmbComponent")) ? "槽钢(21×21×2)" : this.userSetting.GetValue("AddSeismicComponent", "cmbComponent"));
		}

		private void Initializate()
		{
			this.InitializateCmbComponent();
			this.chkLocationA.Checked = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "chkLocationA")) || Convert.ToBoolean(this.userSetting.GetValue("AddSeismicComponent", "chkLocationA")));
			this.chkLocationB.Checked = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "chkLocationB")) || Convert.ToBoolean(this.userSetting.GetValue("AddSeismicComponent", "chkLocationB")));
			this.chkLocationC.Checked = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "chkLocationC")) || Convert.ToBoolean(this.userSetting.GetValue("AddSeismicComponent", "chkLocationC")));
			this.chkLocationD.Checked = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "chkLocationD")) || Convert.ToBoolean(this.userSetting.GetValue("AddSeismicComponent", "chkLocationD")));
			this.chkLocationE.Checked = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "chkLocationE")) || Convert.ToBoolean(this.userSetting.GetValue("AddSeismicComponent", "chkLocationE")));
			this.chkLocationF.Checked = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "chkLocationF")) || Convert.ToBoolean(this.userSetting.GetValue("AddSeismicComponent", "chkLocationF")));
			this.hyTxtAngleA.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "hyTxtAngleA")) ? "45" : this.userSetting.GetValue("AddSeismicComponent", "hyTxtAngleA"));
			this.hyTxtAngleB.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "hyTxtAngleB")) ? "45" : this.userSetting.GetValue("AddSeismicComponent", "hyTxtAngleB"));
			this.hyTxtAngleC.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "hyTxtAngleC")) ? "45" : this.userSetting.GetValue("AddSeismicComponent", "hyTxtAngleC"));
			this.hyTxtAngleD.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "hyTxtAngleD")) ? "45" : this.userSetting.GetValue("AddSeismicComponent", "hyTxtAngleD"));
			this.hyTxtAngleE.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "hyTxtAngleE")) ? "45" : this.userSetting.GetValue("AddSeismicComponent", "hyTxtAngleE"));
			this.hyTxtAngleF.Text = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "hyTxtAngleF")) ? "45" : this.userSetting.GetValue("AddSeismicComponent", "hyTxtAngleF"));
			this.tabHangerType.SelectedIndex = (string.IsNullOrEmpty(this.userSetting.GetValue("AddSeismicComponent", "hangerType")) ? 0 : Convert.ToInt32(this.userSetting.GetValue("AddSeismicComponent", "hangerType")));
		}

		private void LoadPreviwImage()
		{
			this.picPreviewMutil.Image = HangerSupport.Properties.Resources.MultiLayerHanger;
			this.picPreviewSteel.Image = HangerSupport.Properties.Resources.MadeHanger;
			this.picSinglePipe.Image = HangerSupport.Properties.Resources.SingleTubeHanger;
		}

		private void WriteData()
		{
			this.userSetting.WriteValue("AddSeismicComponent", "cmbComponent", this.cmbComponent.Text);
			this.userSetting.WriteValue("AddSeismicComponent", "hangerType", this.tabHangerType.SelectedIndex.ToString());
			this.userSetting.WriteValue("AddSeismicComponent", "chkLocationA", this.chkLocationA.Checked.ToString());
			this.userSetting.WriteValue("AddSeismicComponent", "chkLocationB", this.chkLocationB.Checked.ToString());
			this.userSetting.WriteValue("AddSeismicComponent", "chkLocationC", this.chkLocationC.Checked.ToString());
			this.userSetting.WriteValue("AddSeismicComponent", "chkLocationD", this.chkLocationD.Checked.ToString());
			this.userSetting.WriteValue("AddSeismicComponent", "chkLocationE", this.chkLocationE.Checked.ToString());
			this.userSetting.WriteValue("AddSeismicComponent", "chkLocationF", this.chkLocationF.Checked.ToString());
			this.userSetting.WriteValue("AddSeismicComponent", "hyTxtAngleA", this.hyTxtAngleA.Text.Trim());
			this.userSetting.WriteValue("AddSeismicComponent", "hyTxtAngleB", this.hyTxtAngleB.Text.Trim());
			this.userSetting.WriteValue("AddSeismicComponent", "hyTxtAngleC", this.hyTxtAngleC.Text.Trim());
			this.userSetting.WriteValue("AddSeismicComponent", "hyTxtAngleD", this.hyTxtAngleD.Text.Trim());
			this.userSetting.WriteValue("AddSeismicComponent", "hyTxtAngleE", this.hyTxtAngleE.Text.Trim());
			this.userSetting.WriteValue("AddSeismicComponent", "hyTxtAngleF", this.hyTxtAngleF.Text.Trim());
			this.WriteSeismicComponentSize();
		}

		private void WriteSeismicComponentSize()
		{
			this.userSetting.WriteValue("AddSeismicComponent", "WidthB", this.hyTxtWidthB.Text);
			this.userSetting.WriteValue("AddSeismicComponent", "HightH", this.hyTxtHightH.Text);
			this.userSetting.WriteValue("AddSeismicComponent", "ThickT", this.hyTxtThickT.Text);
		}

		private void DlgAddSeismicComponent_MouseLeave(object sender, EventArgs e)
		{
			bool flag = !base.ClientRectangle.Contains(Cursor.Position);
			if (flag)
			{
				bool flag2 = this.CheckData();
				if (flag2)
				{
					this.SendSetfocusMessage();
				}
			}
			else
			{
				base.Focus();
			}
		}

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			DlgAddSeismicComponent.SendMessage(cmdAddSeismicComponent.wndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private bool CheckData()
		{
			System.Windows.Forms.Control activeControl = base.ActiveControl;
			System.Windows.Forms.TextBox textBox = activeControl as System.Windows.Forms.TextBox;
			bool flag = textBox != null;
			if (flag)
			{
				bool flag2 = textBox.Name.Contains("Angle");
				if (flag2)
				{
					double num;
					bool flag3 = double.TryParse(textBox.Text, out num) && num >= 22.5 && num <= 67.5;
					if (flag3)
					{
						return true;
					}
					YJKMessageBox.Information("“抗震组件角度”需是在22.5°～67.5°范围内的有效数字!");
					textBox.Focus();
					return false;
				}
				else
				{
					double num;
					bool flag4 = textBox.Enabled && !double.TryParse(textBox.Text, out num);
					if (flag4)
					{
						YJKMessageBox.Information("您输入的“抗震组件尺寸”不满足创建要求，无法生成抗震组件!");
						textBox.Focus();
						return false;
					}
				}
			}
			return true;
		}

		private SeismicComponentModel scm;

		private bool cmdEsc = false;

		private UserSetting userSetting = null;

		private Dictionary<string, Image> dicPreviewIamge = new Dictionary<string, Image>();

		private HSType hsType;

		private bool beLoad = true;
	}
}
